package mlc.om;

import java.awt.Point;

import mlc.Constants;
import mlc.Direction;
import mlc.Utils;
import mlc.om.upgrade.UpgradeProvider;
import mlc.om.upgrade.cargo.Cargo;
import mlc.om.upgrade.drill.DrillInterface;
import mlc.om.upgrade.engine.Engine;
import mlc.om.upgrade.hull.Hull;
import mlc.om.upgrade.tank.Tank;

/**
 * Questa e la classe del veicolo spaziale
 * @author Administrator
 */
public class Vehicle {

	private Hull hull;
	private Tank tank;
	private Cargo cargo;
	private DrillInterface drill;
	private Engine engine;

	private Point globalPosition;
	private double speedX, speedY;
	

	public Vehicle() {
		this.hull = UpgradeProvider.hulls.get(0);
		this.tank = UpgradeProvider.tanks.get(0);
		this.cargo = UpgradeProvider.cargos.get(0);
		this.drill = UpgradeProvider.drills.get(0);
		this.engine = UpgradeProvider.engines.get(0);
		
		globalPosition = new Point(0,0);
		speedX = 0;
		speedY = 0;
	}
	
	public void applyGravitySpeed(){
		double[] speeds = Utils.applySpeed(getSpeedY(), Constants.planetGravitySpeedIncrease, Constants.planetGravitySpeedLimit);
		//System.out.println("Gravity speed increased from "+getSpeedY()+" to "+speeds[0]);
		this.speedY = speeds[0];
		//return speeds[1];
	}

	public void applyHorizzontalFriction() {
		if(	speedX > 0 && speedX <= Constants.planetFrictionMinimalSpeed ||
			speedX < 0 && speedX >= -Constants.planetFrictionMinimalSpeed)
			speedX = 0;
		else
			speedX = speedX*(1-Constants.planetFriction);
	}
	
	public void applyEngineSpeed(Direction direction){
		
		double[] speeds;
		
		switch (direction) {
		case LEFT:
			speeds = Utils.applySpeed(getSpeedX(), -getEngine().getSpeedIncrease(), getEngine().getSpeedLimit());
			System.out.println(getEngine().getName()+" moved LEFT from speed "+getSpeedX()+" to speed "+speeds[0]);
			this.speedX = speeds[0];
			break;
		case RIGHT:
			speeds = Utils.applySpeed(getSpeedX(), getEngine().getSpeedIncrease(), getEngine().getSpeedLimit());
			System.out.println(getEngine().getName()+" moved RIGHT from speed "+getSpeedX()+" to speed "+speeds[0]);
			this.speedX = speeds[0];
			break;
		case UP:
			speeds = Utils.applySpeed(getSpeedY(), -getEngine().getSpeedIncrease(), getEngine().getSpeedLimit());
			System.out.println(getEngine().getName()+" moved UP from speed "+getSpeedY()+" to speed "+speeds[0]);
			this.speedY = speeds[0];
			break;
		}
	}
	
	public void clearSpeed(Direction direction){
		switch (direction) {
		case UP: 
		case DOWN:
			this.speedY = 0;
			break;
		case RIGHT:
		case LEFT:
			this.speedX = 0;
			break;
		}
	}
	
	public Hull getHull() {
		return hull;
	}

	public void setHull(Hull hull) {
		this.hull = hull;
	}

	public Tank getTank() {
		return tank;
	}

	public void setTank(Tank tank) {
		this.tank = tank;
	}

	public Cargo getCargo() {
		return cargo;
	}

	public void setCargo(Cargo cargo) {
		this.cargo = cargo;
	}

	public DrillInterface getDrill() {
		return drill;
	}

	public void setDrill(DrillInterface drill) {
		this.drill = drill;
	}

	public Engine getEngine() {
		return engine;
	}

	public void setEngine(Engine engine) {
		this.engine = engine;
	}

	public double getSpeedX() {
		return speedX;
	}

	public double getSpeedY() {
		return speedY;
	}

	public Point getGlobalPosition() {
		return globalPosition;
	}

	public void setGlobalPosition(Point globalPosition) {
		this.globalPosition = globalPosition;
	}
}
